package com.spring.self.tag.alias;


import com.spring.self.tag.alias.dao.UserRepository;
import com.spring.self.tag.alias.service.MemberService;
import com.spring.self.tag.alias.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * import 属性：
 * 用于将多个XML配置文件组合成一个整体配置
 * 路径可以是相对路径或绝对路径
 * 按照导入顺序加载，后面的配置可以覆盖前面的同名Bean定义
 *
 * 容器加载过程：
 * ClassPathXmlApplicationContext 会自动处理所有import的配置文件
 * 所有配置文件中的Bean定义会被合并到一个上下文中
 */
public class AliasTagApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("tag/alias/beans.xml");

        // 通过原始名称获取Bean
        UserRepository repo1 = (UserRepository) context.getBean("userRepository");
        repo1.save();

        // 通过别名获取Bean
        UserRepository repo2 = (UserRepository) context.getBean("memberRepository");
        repo2.save();

        UserRepository repo3 = (UserRepository) context.getBean("accountRepository");
        repo3.save();

        // 验证是否是同一个实例
        System.out.println("repo1 == repo2: " + (repo1 == repo2)); // true
        System.out.println("repo1 == repo3: " + (repo1 == repo3)); // true

        // 测试依赖注入
        UserService userService = context.getBean("userService", UserService.class);
        userService.process();

        MemberService memberService = context.getBean("memberService", MemberService.class);
        memberService.handle();
    }
}